package cn.yunhe.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import cn.yunhe.model.Area;
import cn.yunhe.model.House;
import cn.yunhe.model.Street;
import cn.yunhe.model.Type;
import cn.yunhe.model.User;
import cn.yunhe.services.HouseServices;
import cn.yunhe.util.BaseAction;

/**
 * 
 * @author Administrator
 *
 */
public class FaBuAction extends BaseAction {

	private House house;
	private String userid;
	private String typeid;
	private String streetid;
	private String areaid;

	/* private String date; */
	/* 上传照片 */
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;

	@Override
	public String execute() throws Exception {
		HouseServices hs = new HouseServices();
		User user = new User();
		user.setUserid(Integer.parseInt(userid));
		Type type = new Type();
		type.setTypeid(Integer.parseInt(typeid));
		Area area = new Area();
		area.setAreaid(Integer.parseInt(areaid));
		Street street = new Street();
		street.setArea(area);
		street.setStreetid(Integer.parseInt(streetid));
		hs.addHouse(new House(user, type, street, house.getTitle(), house.getContact(), uploadFileName,
				house.getPrice(), house.getPubdate(), house.getFloorage(), house.getContact()));
		String uploadPath = "C://Users//Administrator//git//ZuFangXiTong//src//main//webapp//"+getSavePath() + "/" + uploadFileName;
		doUpload(upload, uploadPath);
		return SUCCESS;
	}

	public void doUpload(File file, String uploadFilePath) throws Exception {
		System.out.println(uploadFilePath);
		FileOutputStream fos = new FileOutputStream(uploadFilePath);
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) != -1) {
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();
	}

	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public String getStreetid() {
		return streetid;
	}

	public void setStreetid(String streetid) {
		this.streetid = streetid;
	}

	public String getAreaid() {
		return areaid;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

}
